
#ifndef MBED_LFSFILESYSTEM_H
#define MBED_LFSFILESYSTEM_H
//#include "comm_type.h"
//#include <stdint.h>
//#include "dll_comm_type.h"
#include "ThirdBasics.h"

#ifdef __cplusplus
extern "C" {
#endif

#ifndef offsize
#define 	offsize(bak,item)	(sizeof(((bak *)0)->item))
#endif
#define		DfGetBeiSu(nL,n)	(((nL)+(n-1))&(~(n-1)))	//设成整数倍长度2/4/8/16/32...

#define 	ARRAY_NUM(st) 	(sizeof(st)/sizeof(st[0]))


#define GFS_VERSION 	"3.02"
#define GTREE_FILE_COURCE

#define WINDOW_FLASH_SOFT

//----检查原块是否可以写入---
#define XOR_CHECK_SAVE	


#include "gfs_flash.h"
#include "gfs.h"
#include "gfs_BR.h"
//#include "gfs_api.h"

// Possible error codes, these are negative to allow
// valid positive return values
enum lfs_error {
    GFS_OK      	= 0,    // No error
    
    GFS_ERR_DIR      	= -2,   // Error during device operation
	GFS_ERR_NORD_PER	= -4,	// No write permission
	GFS_ERR_NOWR_PER	= -5,	// No write permission
	GFS_ERR_LEN			= -7,	// No write permission
	GFS_CRE_NOUSE		= -11,  // 创建而没有使用
	GFS_WRT_NOOK		= -12,  //写入没有成功
	GFS_ERR_NOSPC		= -13,  // No space left on device
};

// File open flags(Compatible with OpenHarmony)
enum lfs_open_flags {
	GFS_O_RDONLY	= 0,        // Open a file as read only
	GFS_O_WRONLY	= 1,        // Open a file as write only
	GFS_O_RDWR		= 2,        // Open a file as read and write
	GFS_O_APPEND	= 0x0008,   // append (writes guaranteed at the end)
	GFS_O_EXCL		= 0x0800,   ///error on open if file exists 
	GFS_O_CREAT		= 0x0200,   // Create a file if it does not exist
	GFS_O_TRUNC		= 0x0400,   // open with truncation
	//------Custom extension-----------
	GFS_O_ENC		= 0x0800,   // Encrypt file
	GFS_O_COUNT		= 0x1000,   // Counter file
};

#define F_CHECK_WR(flag) ((flag)&(GFS_O_RDWR|GFS_O_WRONLY)) 

// File seek flags
enum lfs_whence_flags {
    GFS_SEEK_SET = 0,   // Seek relative to an absolute position
    GFS_SEEK_CUR = 1,   // Seek relative to the current file position
    GFS_SEEK_END = 2,   // Seek relative to the end of the file
};

extern int gfs_remove(const char *path);
extern gFILE gfs_open(const char *path, int flags ,...);
extern int gfs_seek(gFILE pfd,int offset, int whence);
extern int gfs_read(gFILE pfd,void *buff,int size);
extern int gfs_write(gFILE pfd,void *buff,int writeLen);
extern int gfs_sync(gFILE pfd);	//将文件同步到磁盘
extern int gfs_close(gFILE pfd);
extern int gfs_GetSize(gFILE pfd);
extern int gfs_ReadSize(const char *path);
extern int gfs_rename(const char *oldpath, const char*newpath);
//=============文件系简化定义=======================
#define	F_RDONLY	GFS_O_RDONLY		//!< 只读
#define	F_WRONLY	GFS_O_WRONLY		//!< 只写
#define	F_RDWR		GFS_O_RDWR		//!< 读写
#define	F_EXCL		GFS_O_EXCL		//当文件存时打开文件返回失败
#define	F_APPEND	GFS_O_APPEND	//!< 追加到尾部
#define	F_CREAT		GFS_O_CREAT		//!< 自动创建
#define	F_CRNEW		GFS_O_TRUNC		//!< 删除当前，创建新的

#define	F_ENC		GFS_O_ENC		//!< 加密文件 ，后面变参为密码
#define	F_CONT		GFS_O_COUNT		//!< 流水记录 ，后面变参为单位长度与总数
/**
 * 文件seek模式
 */
#define	F_SEEK_SET	GFS_SEEK_SET		//!< 文件开头
#define	F_SEEK_CUR	GFS_SEEK_CUR		//!< 当前位置
#define	F_SEEK_END	GFS_SEEK_END		//!< 文件结尾

#define API_fopen(name,flag,...) 	gfs_open(name,flag,## __VA_ARGS__)
#define API_fclose(fd)				gfs_close(fd)
#define API_fseek(fd,offset,whence)	gfs_seek(fd,offset,whence)
#define API_fread(fd,buf,count)		gfs_read(fd,buf,count)
#define API_fwrite(fd,buf,count)	gfs_write(fd,buf,count)
#define API_fremove(name)			gfs_remove(name)
#define API_fsize(name)				gfs_ReadSize(name)


#ifdef __cplusplus
}
#endif

#endif
